package org.lego.biz.task.management.admin.api.restful;

import org.lego.biz.task.management.admin.api.ember.EmberModel;
import org.lego.biz.task.management.base.entity.Project;
import org.lego.biz.task.management.base.entity.Task;
import org.lego.biz.task.management.base.entity.TaskModule;
import org.lego.biz.task.management.base.service.ProjectService;
import org.lego.biz.task.management.base.service.TaskModuleService;
import org.lego.biz.task.management.base.service.TaskService;
import org.lego.jpa.base.util.DynamicSpecifications;
import org.lego.jpa.base.util.ServletUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by hr.qjy on 16/11/15.
 */
@RestController
@RequestMapping(value = "/admin/project")
public class ProjectAdminRestful {

    @Autowired
    private ProjectService projectService;
    @Autowired
    private TaskModuleService taskModuleService;
    @Autowired
    private TaskService taskService;

    @CrossOrigin(origins = "http://localhost:4200")
    @RequestMapping(value = "/projects", method = RequestMethod.GET)
    public EmberModel list(@RequestParam(value = "page", defaultValue = "1") int pageNumber,
                           @RequestParam(value = "rows", defaultValue = "10") int pageSize,
                           ServletRequest request) {
        PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, ServletUtil.getParametersStartingWithSort(request));
        Map<String, Object> queryMap = ServletUtil.getParametersStartingWithSearch(request);
        queryMap.put("EQ_status", Project.STATUS_NORMAL);
        Specification<Project> specification = DynamicSpecifications.buildSpecification(queryMap, Project.class);
        List<Project> projects = projectService.findAll(specification, pageRequest).getContent();
        return new EmberModel.Builder<Project>(Project.class, projects)
                .build();
    }

    @CrossOrigin(origins = "http://localhost:4200")
    @RequestMapping(value = "/projects/{id}", method = RequestMethod.GET)
    public EmberModel findOne(@PathVariable("id") String id) {
        Project project = projectService.findOneNormal(id);
        TaskModule taskModule = project.getTaskModule();
        // 查询任务 未完成,未指派,未开始 默认查10条
        taskModule.setTasks(taskService.getFirstPage(taskModule.getId()));
        // 查询清单

        return new EmberModel.Builder<Project>(project)
                .sideLoad(TaskModule.class, taskModule)
                .sideLoad(Task.class, taskModule.getTasks())
                .addMeta("total", "测试meta")
                .build();
    }

    @CrossOrigin(origins = "http://localhost:4200")
    @RequestMapping(value = "/projects/{id}", method = RequestMethod.DELETE)
    public EmberModel deleteOne(@PathVariable("id") String id) {
        Project project = projectService.findOne(id);
        if (!ObjectUtils.isEmpty(project)) {
            //projectService.delete(id);
            project.setStatus(Project.STATUS_DELETE);
            projectService.saveAndFlush(project);
        }
        return new EmberModel.Builder<Project>().build();
    }


    @CrossOrigin(origins = "http://localhost:4200")
    @RequestMapping(value = "/projects", method = RequestMethod.POST)
    public EmberModel insertOne(@RequestBody Map<String, Project> map) {
        TaskModule taskModule = new TaskModule();
        taskModule = taskModuleService.saveAndFlush(taskModule);
        Project project = map.get("project");
        if (!ObjectUtils.isEmpty(project)) {
            project.setTaskModule(taskModule);
            project = projectService.saveAndFlush(project);
        }
        return new EmberModel.Builder<Project>(project)
                .build();
    }

    @CrossOrigin(origins = "http://localhost:4200")
    @RequestMapping(value = "/projects/{id}", method = RequestMethod.PUT)
    public EmberModel updateOne(@PathVariable("id") String id, @RequestBody Map<String, Project> map) {
        Project project = projectService.findOneNormal(id);
        if (!ObjectUtils.isEmpty(project)) {
            BeanUtils.copyProperties(map.get("project"), project, "id");
            projectService.saveAndFlush(project);
        }
//        projectService.saveAndFlush(vo.getProject());
        return new EmberModel.Builder<Project>(project).build();
    }

    @CrossOrigin(origins = "http://localhost:4200")
    @RequestMapping(value = "/projects/{id}/tasks", method = RequestMethod.GET)
    public EmberModel getTasks(@PathVariable("id") String id) {
        Project project = projectService.findOneNormal(id);
        TaskModule taskModule = project.getTaskModule();
        // 查询任务 未完成,未指派,未开始 默认查10条
        List<Task> tasks = taskService.getFirstPage(taskModule.getId());

        return new EmberModel.Builder<Task>(Task.class, tasks)
                //.sideLoad(Task.class, taskModule.getTasks())
                .addMeta("total", String.valueOf(taskService.getTotal(taskModule.getId())))
                .build();
    }


}
